package cn.z2huo.knowledge.concurrency.callable.callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * <p>
 *
 * @author z2huo
 */
class FutureIsDoneTest {

    public static void main(String[] args) {

        long start, end;

        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Future<String>> results = new ArrayList<>();

        start = System.currentTimeMillis();

        for (int i = 0; i < 10; i++) {
            results.add(executorService.submit(new MyCallable(i)));
        }
        for (Future<String> result : results) {
            try {
                while (!result.isDone()) {
                    TimeUnit.MILLISECONDS.sleep(10);
                }
                System.out.println(result.get());
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        executorService.close();

        end = System.currentTimeMillis();
        System.out.println("cost time: " + (end - start));
    }

}
